package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;


    //查询小视频列表
    @GetMapping("/smallVideos")
    public PageBeanVo findVideo(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize
    ) {

        //获取登录用户的id
        Long userId = UserHolder.get().getId();

        return videoManager.findRecommendVideo(userId, pageNum, pageSize);

    }


    //发布小视频
    @PostMapping("/smallVideos")
    public void saveVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {

        //获取登录用户的id
        Long userId = UserHolder.get().getId();

        videoManager.saveVideo(userId, videoThumbnail, videoFile);

    }


    //小视频用户关注
    @PostMapping("/smallVideos/{uid}/userFocus")
    public void saveUserFocus(@PathVariable("uid") Long focusUserId) {

        //获取登录用户的id,清缓存用
        Long userId = UserHolder.get().getId();
        videoManager.userFocus(focusUserId,userId);

    }


    //删除小视频用户关注
    @PostMapping("/smallVideos/{uid}/userUnFocus")
    public void deleteUserFocus(@PathVariable("uid") Long focusUserId) {

        //获取登录用户的id,清缓存用
        Long userId = UserHolder.get().getId();

        videoManager.userUnFocus(focusUserId,userId);

    }

    //视频点赞
    @PostMapping("/smallVideos/{id}/like")
    public void smallVideosLike(@PathVariable("id")String videoId) {
        //获取登录用户的id,清缓存用
        Long userId = UserHolder.get().getId();
        videoManager.likeVideo(videoId,userId);
    }

    //视频取消赞
    @PostMapping("/smallVideos/{id}/dislike")
    public void smallVideosDislike(@PathVariable("id")String videoId) {
        videoManager.dislikeVideo(videoId);
    }

    //评论列表
    @GetMapping("/smallVideos/{id}/comments")
    public PageBeanVo videosCommentsList(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize,
                                         @PathVariable("id")String videoId) {
        return videoManager.videosCommentsList(videoId, pageNum, pageSize);
    }

    //评论发布
    @PostMapping("/smallVideos/{id}/comments")
    public void saveVideoComment(@RequestBody Map<String,String> map,@PathVariable("id")String videoId) {
        String comment = map.get("comment");
        Long userId = UserHolder.get().getId();
        videoManager.saveVideoComment(videoId, comment,userId);
    }

    //评论点赞
    @PostMapping("/smallVideos/comments/{id}/like")
    public void commentsLike(@PathVariable("id") String commentId) {
        System.out.println("点赞传入id"+commentId);
        videoManager.videoCommentsLike(commentId);
    }
    //评论点赞取消
    @PostMapping("/smallVideos/comments/{id}/dislike")
    public void commentsDislike(@PathVariable("id") String commentId) {
        System.out.println(commentId);
        videoManager.videoCommentsDislike(commentId);
    }
}
